package com.sqx.modules.coupon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.coupon.entity.TbCoupon;
import com.sqx.modules.coupon.service.TbCouponService;
import com.sqx.modules.coupon.service.TbCouponUserService;
import com.sqx.modules.datacentre.dao.ShopAdminDao;
import com.sqx.modules.datacentre.entity.SysUserShop;
import com.sqx.modules.sys.controller.AbstractController;
import com.sqx.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@Api(value = "管理端-优惠券", tags = {"管理端-优惠券"})
@RequestMapping(value = "/admin/coupon")
public class CouponController extends AbstractController {

    @Autowired
    private TbCouponService tbCouponService;
    @Autowired
    private ShopAdminDao shopAdminDao;

    @ApiOperation("查看发布的优惠券")
    @GetMapping(value = "seleteAllCoupon")
    public Result seleteAllCoupon(Integer page, Integer limit){

        return tbCouponService.seleteAllCoupon(page, limit);
    }

    @ApiOperation("商铺管理员查看自己商铺下的所有优惠券")
    @GetMapping(value = "selectCouponByShopId")
    public Result selectCouponByShopId(Integer page, Integer limit){
        SysUserEntity user = getUser();
        Long userId = user.getUserId();
        List<SysUserShop> userShops = shopAdminDao.selectList(new QueryWrapper<SysUserShop>().eq("user_id", userId));
        return tbCouponService.selectCouponByShopId(page, limit, userShops);
    }

    @ApiOperation("发布优惠券")
    @PostMapping(value = "issueCoupon")
    public Result issueCoupon(@RequestBody TbCoupon tbCoupon){

        return tbCouponService.issueCoupon(tbCoupon);
    }

    @ApiOperation("删除发布的优惠券")
    @PostMapping(value = "deleteCoupon")
    public Result deleteCoupon(Long couponId){

        return tbCouponService.deleteCoupon(couponId);
    }

    @ApiOperation("修改优惠券信息")
    @PostMapping(value = "updateCoupon")
    public Result updateCoupon(@RequestBody TbCoupon tbCoupon){

        return tbCouponService.updateCoupon(tbCoupon);
    }

    @ApiOperation("管理端赠送用户优惠券")
    @GetMapping(value = "giveCoupon")
    public Result giveCoupon(Long couponId, String userIds, Long shopId){
        String[] userId = userIds.split(",");
        List<String> userIdList = Arrays.asList(userId);
        return tbCouponService.giveCoupon(couponId, userIdList, shopId);
    }

}